﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WarehouseWebAPP.code.DAL;


namespace WarehouseWebAPP.code.BLL
{
    public class Mail
    {
        static public Boolean SendMail(string address, string subject, string body)
        {
            try
            {
                ParameterBLL BLL = new ParameterBLL();
                string MailAddress = BLL.ParameterGetByName("mail_address")[0].parameter_value;
                string MailPassword = BLL.ParameterGetByName("mail_password")[0].parameter_value;
                string SMTPServer = BLL.ParameterGetByName("smtp_server")[0].parameter_value;


                System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                System.Net.NetworkCredential cred = new System.Net.NetworkCredential(MailAddress, MailPassword);

                mail.To.Add(address);
                mail.Subject = subject;

                mail.From = new System.Net.Mail.MailAddress(MailAddress);
                mail.IsBodyHtml = true;

                mail.Body = Mail.Title() + body + Mail.MailFooter();

                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(SMTPServer);
                smtp.UseDefaultCredentials = false;
                smtp.EnableSsl = true;
                smtp.Credentials = cred;
                smtp.Port = 25; //587?
                smtp.EnableSsl = true;
                smtp.Send(mail);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
            finally
            {
                //TODO: Dispose
            }
        }

        static public string HTMLEntityDecorator(string title, string[] fields, string[] values)
        {
            if (values.Length != fields.Length) return "";
            string DecoratedString = "<span style=\"color: #69748C; font-weight: bold; font-size: 12pt; text-decoration: underline\">";
            DecoratedString += title;
            DecoratedString += "</span><br />\r\n";
            DecoratedString += "<ul>";
            for (int i = 0; i < fields.Length; i++)
            {
                DecoratedString += "<li><span style=\"color:#5E5E5E; font-weight: bold\">";
                DecoratedString += fields[i] + ": " + "</span>" ;
                DecoratedString += "<span style=\"color:#5E5E5E\">";
                DecoratedString += values[i] + "</span></li>" + "<br />\r\n";
            }
            DecoratedString+="</ul>";
            return DecoratedString;
        }

        static public string MailFooter()
        {

            return "<br />\r\n<br />\r\n--<br />\r\nRegrads,<br />\r\nThe WareHouse Staff";
        }

        static public string SenderNotes(string notes)
        {
            return "<span style=\"color: #AC0717; font-weight: bold; text-decoration: underline\">Sender Notes:<br />\r\n</span><span style=\"color: #000066; font-style: italic\">" + notes + "</span>\r\n<br />\r\n<br />";
        }

        static public string Title()
        {
            string title="<span style=\"color: #666666; font-family: georgia,times new roman,times,serif\">The WareHouse Application</span><br />";
            title += "<span style=\"font-size: 8pt; color: #666666; font-family: georgia,times new roman,times,serif\">Mail Delivery System<br />";
            title+="<span style=\"text-decoration: line-through\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><br /><br />";
            return title;
        }
    }
}
